<?php
set_time_limit(0);
$root=preg_replace('/cron$/', '', dirname(__FILE__));

require_once($root."inc.config.php");

require_once($root."functions/common/common.php");

require_once($root."functions/general_function.php");

require_once($root."functions/downline/downline_function.php");

require_once($root."functions/comm/comm_function.php");

require_once($root."initialize.php");

if(($db_msg=cron_initialize()) !== true){

	$errmsg="Error starting rebuild tree";

	$a_sub="Error starting rebuild tree";

	$a_msg="<p>Dear admin,</p>$errmsg";

	//email_admin($a_sub, $a_msg, 'e');

}else{

	$db=$vars["db"];

	rebuild(1);
	rebuild2(1);
}

	global $arrTree;
	function rebuild($rootid=1)
	{
		$t = @mysql_num_rows($r=mysql_query("select id,matrix_upline from gg_users where matrix_upline>0 or id=1 order by id"));
		
		if($t<=0)
		{
			return false;
		}
		$arrTree = array();
		while($user = mysql_fetch_assoc($r))
		{
			$nParentID = $user['matrix_upline'];
			$dataTemp  = array();
			$dataTemp['id']    = $user['id'];
			$arrTree[$nParentID][] = $dataTemp;
		}

		$rootID = 0;
		if((isset($arrTree[0])) && (sizeof($arrTree[0])>0) )
		{
			$rootID = $arrTree[0][0]['id'];
		}

		if($rootID<=0)
		{
			return false;
		}

		rebuild_tree($rootID,1,1,1, $arrTree);
		return true;

	}

	function rebuild_tree($nParentID,$left,$nlevel,$seq, $arrTree)
	{
		$right = $left+1;

		$subNodeInfo   = @(isset($arrTree[$nParentID]))?$arrTree[$nParentID]:array();

		$subSize       = sizeof($subNodeInfo);
		if($subSize>0)
		$isparent = 1;
		else
		$isparent=0;

		if($subSize>0)
		{
			$seqno = 0;
			foreach($subNodeInfo as $key=>$nodeInfo)
			{
				$right = rebuild_tree($nodeInfo['id'],$right,$nlevel+1,$seqno+1, $arrTree);
				$seqno++;
			}
		}

		mysql_query("update gg_users set matrix_left = $left, matrix_right = $right, matrix_level = $nlevel where id = $nParentID");
		
		return $right+1;
	}

	global $arrTree2;
	function rebuild2($rootid=1)
	{
		$t = @mysql_num_rows($r=mysql_query("select id,ref from gg_users where ref>0 or id=1 order by id"));
		
		if($t<=0)
		{
			return false;
		}
		$arrTree2 = array();
		while($user = mysql_fetch_assoc($r))
		{
			$nParentID = $user['ref'];
			$dataTemp  = array();
			$dataTemp['id']    = $user['id'];
			$arrTree2[$nParentID][] = $dataTemp;
		}

		$rootID = 0;
		if((isset($arrTree2[0])) && (sizeof($arrTree2[0])>0) )
		{
			$rootID = $arrTree2[0][0]['id'];
		}

		if($rootID<=0)
		{
			return false;
		}

		rebuild_tree2($rootID,1,1,1, $arrTree2);
		return true;

	}

	function rebuild_tree2($nParentID,$left,$nlevel,$seq, $arrTree2)
	{
		$right = $left+1;

		$subNodeInfo   = @(isset($arrTree2[$nParentID]))?$arrTree2[$nParentID]:array();

		$subSize       = sizeof($subNodeInfo);
		if($subSize>0)
		$isparent = 1;
		else
		$isparent=0;

		if($subSize>0)
		{
			$seqno = 0;
			foreach($subNodeInfo as $key=>$nodeInfo)
			{
				$right = rebuild_tree2($nodeInfo['id'],$right,$nlevel+1,$seqno+1, $arrTree2);
				$seqno++;
			}
		}

		mysql_query("update gg_users set ref_left = $left, ref_right = $right, ref_level = $nlevel where id = $nParentID");
		
		return $right+1;
	}
?>